//
//  HMVistorView.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/8.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit
//定义代理
protocol HMVistorViewDelegate:NSObjectProtocol{
//定义代理方法
    func visitorViewDidClickRegisterButton()
    
    func visitorViewDidClickLoginButton()



}


class HMVistorView: UIView {

    //代理属性
    weak var delegate: HMVistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        prepareUI()

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    
    //MARK - 公开方法
    func setupVistorViewInfo(imageName:String,message:String){
    
        iconView.image = UIImage.init(named: imageName)
        
        messageLabel.text = message
        
    //设置房子隐藏
//        homeView.h
        homeView.hidden = true

    
    }
    //核心动画
    func startRoutate(){
    //使用核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = M_PI * 2
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration  = 20
        
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    private func  prepareUI(){
    //添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        //禁用Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false

        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registButton.translatesAutoresizingMaskIntoConstraints = false
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
    
        
        
        //添加约束
        //圈圈约束
        self.addConstraint(NSLayoutConstraint(item: self.iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: self.iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        /// 遮盖
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        /// 房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /// 消息label
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        /// 当参照的view为nil时,attribute必须设置为NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        /// 注册按钮
        /// 阿拉伯语言
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /// 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        
    }
   //MARK: - 代理方法
    
    @objc private func didClickResigisterButton(){
    
    delegate?.visitorViewDidClickRegisterButton()
    }
    @objc private func didClickLoginButton(){
        
        delegate?.visitorViewDidClickLoginButton()

    }
    
    
    //MARK: - 懒加载
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    //消息文字
    private lazy var messageLabel: UILabel = {
     let label = UILabel()
       
        //字体设置
        label.text = "关注一些人!看看有什么惊喜!"
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        label.textAlignment = .Center
        return label
    
    }()
    
    //注册按钮
    private lazy var registButton: UIButton = {
    let button = UIButton()
    
    button.setTitle("注册", forState: .Normal)
    button.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: .Normal)
    button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
   
    button.addTarget(self, action: Selector("didClickResigisterButton"), forControlEvents: .TouchUpInside)
    return button
    }()
    
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
    button.setTitle("登录", forState: .Normal)
    button.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: .Normal)
    button.setTitleColor(UIColor.blackColor(), forState: .Normal)
    button.addTarget(self, action: Selector("didClickLoginButton"), forControlEvents: .TouchUpInside)
        return button
    }()
    
    //登录注册按钮
    
    
}














